import {isEmpty} from '../utils'
import {addOutput} from "../outputs";

export type Action = {
    type: string ;
    name: string;
    order: number;
    action: string;
}

export const ACTION_TYPE_HIDDEN = "HIDDEN"
export const ACTION_TYPE_EXECUTE = "EXECUTE"

type CallbackAction = {
    type: string ;
    name: string;
    title: string;
    action: string;
    disable: boolean;
    hidden: boolean;
    metadata: {
        order: number;
    };
}

export function runAction(action: string): void {
    if (isEmpty(action)) {
        return
    }
    addOutput("SYSTEM_AGENTLESS_ACTION="+action);
}

// To display your action on the button, choose action.action from ['RESOLVE_CONFLICT', 'RETRY', 'SKIP', 'CONFIRM', 'COMPLETE', 'ABORT', 'MERGE', 'ROLLBACK']. Note: 'CANCEL' is reserved for system.
//
// Order: 40-60, default value: 50. the smaller order will be ranked first.
export function appendActions(actions: Action[]): void {
    // 写入action
    const systemActions : Array<CallbackAction> = [];
    for(let i=0; i< actions.length;i++){
        const action = actions[i];
        if (action.name == "CANCEL"){
            throw new Error(`${action.name} is reserved for system`)
        }
        const callbackAction  : CallbackAction = {
            type: action.type,
            name: action.name,
            title: action.name,
            action: action.action,
            disable: false,
            hidden: action.type === "HIDDEN",
            metadata: {
                order: action.order
            }
        }
        systemActions.push(callbackAction);
    }
    addOutput("SYSTEM_ACTIONS=" + JSON.stringify(systemActions));

}

